package com.Serverce;

import com.DAO.Userinfo;
import com.Util.Window;
import com.Module.User;

import javax.swing.*;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.metal.MetalComboBoxUI;
import java.awt.event.ActionEvent;

public class Enroll {
    public Enroll() {
        JFrame jf = new Window().window("酒店管理系统注册",500,400,500,400);

        String[] nums={"用户","管理员"};
        Box choice=Box.createHorizontalBox();
        JComboBox<String> select=new JComboBox<>(nums);
        ComboBoxUI ui=new MetalComboBoxUI();
        select.setUI(ui);
        JLabel  jLabel=new JLabel("身份选择");
        choice.add(jLabel);
        choice.add(Box.createHorizontalStrut(20));
        choice.add(select);

        Box idBox=Box.createHorizontalBox();
        JLabel idlLabel=new JLabel("编        号");
        JTextField iField=new JTextField(15);

        idBox.add(idlLabel);
        idBox.add(Box.createHorizontalStrut(20));
        idBox.add(iField);

        Box Box_tel=Box.createHorizontalBox();
        JLabel lLabel_tel=new JLabel("联系电话");
        JTextField Field_tel=new JTextField(15);

        Box_tel.add(lLabel_tel);
        Box_tel.add(Box.createHorizontalStrut(20));
        Box_tel.add(Field_tel);

        Box uBox=Box.createHorizontalBox();
        JLabel uLabel=new JLabel("姓        名");
        JTextField uField=new JTextField(15);

        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);

        Box pBox=Box.createHorizontalBox();
        JLabel pLabel=new JLabel("密        码");
        JPasswordField pField=new JPasswordField(15);

        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(pField);

        Box pBox_again=Box.createHorizontalBox();
        JLabel pLabel_again=new JLabel("确认密码");
        JPasswordField pField_again=new JPasswordField(15);

        pBox_again.add(pLabel_again);
        pBox_again.add(Box.createHorizontalStrut(20));
        pBox_again.add(pField_again);

        Box btnbox=Box.createHorizontalBox();
        JButton logiButton=new JButton("注        册");
        logiButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id=iField.getText();
                String name=uField.getText();
                String tel=Field_tel.getText();
                String password=pField.getText();
                String state=select.getSelectedItem().toString();
                User user=new User(Integer.parseInt(id),name,password,tel);
                Object o=(Object) user;
                int ans=new Userinfo().update(o);
                if (id.equals("") || name.equals("") || tel.equals("") || password.equals("") ) {
                    JOptionPane.showMessageDialog(null,"不能输入空值","错误",JOptionPane.WARNING_MESSAGE);
                }else if(state.equals("管理员")){
                    JOptionPane.showMessageDialog(null,"暂未开放","错误",JOptionPane.WARNING_MESSAGE);
                }else {
                    if (ans == 1) {
                        JOptionPane.showMessageDialog(null,"注册成功","成功",JOptionPane.CLOSED_OPTION);
                    }else{
                        JOptionPane.showMessageDialog(null,"注册失败","错误",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        JButton registButton=new JButton("返回登录");
        registButton.addActionListener(e->{new Login();jf.dispose();});

        btnbox.add(logiButton);
        btnbox.add(Box.createHorizontalStrut(50));
        btnbox.add(registButton);

        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(choice);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(idBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(Box_tel);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox_again);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnbox);

        JPanel jp=new JPanel();
        jp.add(vBox);
        jf.add(jp);
        jf.setVisible(true);
    }
}
